package com.wry.netty.handler;



import com.wry.netty.utils.CurrencyUtil;
import com.wry.netty.utils.Message;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <h1> 客户端 将byte转换成Message的转换器 <h1>
 *
 * @author wry
 * @since 2023/4/19 9:20
 */
public class ClientTransferMsgHandler extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        byte[] buff = new byte[1024];
        final int length = in.readableBytes();
        in.readBytes(buff,0,length);

        String totalMsg = new String(buff, 0, length, StandardCharsets.UTF_8);
        String[] content = totalMsg.split("~");
        out.add(new Message(content[0], CurrencyUtil.parseDateTime(content[1]), content[2]));
    }
}
